﻿using ChestPainCenterSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ChestPainCenterSystem.Code.Bll;
using ChestPainCenterSystem.Core;
using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.ThreeCenterManage;

namespace ChestPainCenterSystem.Areas.ChestPainCenter.Controllers
{
    /// <summary>
    /// 诊疗路径事件节点
    /// 诊疗路径模板
    /// </summary>
    public class PathwaySetController : BaseController
    {
        //
        // GET: /ChestPainCenter/PathwaySet/
        /// <summary>
        /// 路径设置
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }

        #region 诊疗路径事件节点
        public ActionResult Search()
        {
            //诊疗路径事件列表
            int pathId = WebUtils.GetInt("pathId");
            DataTable dt = new DataTable();
            dt.Columns.Add("Id");
            dt.Columns.Add("EventName");//
            dt.Columns.Add("IsDel");
            dt.Columns.Add("Sort");
            dt.Columns.Add("BeforeEventNameList");//前面的节点列表
            dt.Columns.Add("ParentId");//当ParentID为0时，是路径节点，非0是对应的诊疗内容
            dt.Columns.Add("BeforeEvent");
            dt.Columns.Add("Minite");
            DataRow dr;
            Random r1 = new Random();
            dr = dt.NewRow();
            dr["EventName"] = "到放射科";
            dr["IsDel"] = 0;
            dr["Sort"] = 0;
            dr["BeforeEventNameList"] = "[{}]";//"[{\"name\":\"发病时间\", \"eid\":\"1\"},{\"name\":\"到达急诊\", \"eid\": \"2\"},{\"name\":\"呼叫医生\", \"eid\": \"3\"},{ \"name\":\"医生到达\", \"eid\": \"4\"}]";
            dr["ParentId"] = 0;
            dr["Id"] = 1;
            dr["BeforeEvent"] = 3;
            dr["Minite"] = 10;
            dt.Rows.Add(dr);
            var res = new { Rows = dt };
            return JsonDate(res);
        }
        public ActionResult EditEvent()
        {
            return View();
        }

        public ActionResult SaveEvent()
        {
            OperateResult result = new OperateResult();
            string pathEventName = WebUtils.GetString("pathEventName");
            string pathEventSort = WebUtils.GetString("pathEventSort");
            return JsonDate(result);
        }
        #endregion

        #region 诊疗模板保存与删除
        /// <summary>
        /// 新增/编辑诊疗内容 自定义项
        /// </summary>
        /// <returns></returns>
        public ActionResult EditContent()
        {
            int pathId = WebUtils.GetInt("pathId");
            ViewBag.Id = pathId;
            PathwayManager.GetDiagnosticPathTemp(pathId, "");
            return View();
        }
        /// <summary>
        /// 保存诊疗内容 自定义项
        /// </summary>
        /// <returns></returns>
        public ActionResult SaveContent()
        {
            int pathId = WebUtils.GetInt("pathId");
            //清除之前的模板
            //获取之前的模板
            OperateResult resultInfo = PathwayManager.GetDiagnosticPathTemp(pathId, "");
            var templateObjList = JsonConvert.DeserializeObject<List<dynamic>>(resultInfo.Body.ToString());           
            ArrayOfInt idList = new ArrayOfInt();
            int Id = 0;
            foreach (var item in templateObjList)
            {
                Id = item.ID;
                idList.Add(Id);
            }
            //开始清除之前的模板
            PathwayManager.DeleteDiagnosticPathTemp(idList);
            //开始保存新的模板
            string str = WebUtils.GetString("results2");
            string[] strArray = str.Split('|');
            DiagnosticPathTemp template = new DiagnosticPathTemp();
            List<DiagnosticPathTemplate> list = new List<DiagnosticPathTemplate>();
            for (int i = 0; i < strArray.Length; i++)
            {
                if (!string.IsNullOrEmpty(strArray[i]))
                {
                    DiagnosticPathTemplate templateDetail = new DiagnosticPathTemplate();
                    string[] item = strArray[i].Split(',');
                    templateDetail.ControlType = Convert.ToInt32(item[0]);
                    //0->data format: ControlType,ControlDescribe,ControlName,ControlTitle|ControlType,ControlDescribe,ControlName,ControlTitle|...                    
                    if (templateDetail.ControlType == 0)
                    {
                        templateDetail.ControlDescribe = item[1];
                        templateDetail.ControlName = item[2];
                        templateDetail.ControlId = item[2];
                        templateDetail.ControlTitle = item[3];
                    }
                    //1->data format: ControlType,Options,ControlName,ControlTitle|ControlType,Options,ControlName,ControlTitle|...                    
                    if (templateDetail.ControlType == 1)
                    {
                        templateDetail.ControlId = item[2];
                        templateDetail.ControlName = item[2];
                        templateDetail.ControlTitle = item[3];
                        templateDetail.Options = item[1];
                    }
                    //2->data format ControlType,Options,ControlName,ControlTitle|ControlType,Options,ControlName,ControlTitle|...
                    if (templateDetail.ControlType == 2)
                    {
                        templateDetail.ControlId = item[2];
                        templateDetail.ControlName = item[2];
                        templateDetail.ControlTitle = item[3];
                        templateDetail.Options = item[1];
                    }
                    //3->data format ControlType,Options,ControlName,ControlTitle|ControlType,Options,ControlName,ControlTitle|...
                    if (templateDetail.ControlType == 3)
                    {
                        templateDetail.ControlId = item[2];
                        templateDetail.ControlName = item[2];
                        templateDetail.ControlTitle = item[3];
                        templateDetail.Options = item[1];
                    }
                    //ControlType,ControlTitle,ControlName,DateType
                    if (templateDetail.ControlType == 4)
                    {
                        templateDetail.ControlId = item[2];
                        templateDetail.ControlName = item[2];
                        templateDetail.ControlTitle = item[1];
                        switch (item[3])
                        {
                            case "0":
                                templateDetail.TimeFormart = "yyyy-MM-dd HH:mm:ss";
                                break;
                            case "1":
                                templateDetail.TimeFormart = "yyyy-MM-dd";
                                break;
                            case "2":
                                templateDetail.TimeFormart = "yyyy-MM";
                                break;
                            case "3":
                                templateDetail.TimeFormart = "yyyy";
                                break;
                            case "4":
                                templateDetail.TimeFormart = "HH:mm:ss";
                                break;
                            default: break;
                        }
                    }
                    templateDetail.ID = 0;
                    templateDetail.IsMust = 0;
                    templateDetail.IsTimeLimit = 0;
                    templateDetail.IsTimePoint = 0;
                    templateDetail.LimitHours = 0;
                    list.Add(templateDetail);
                }
            }
            template.DiagnosticPathId = pathId;
            template.TemplateList = list;
            string contentJson = JsonConvert.SerializeObject(template);
            OperateResult result = PathwayManager.SetDiagnosticPathTemp(contentJson);
            return JsonDate(result);
        }
        #endregion
    }
}
